local skel = fk.CreateSkill {
  name = "lb_zero__xiasheng",
  max_branches_use_time = function(self, player)
    local room = Fk:currentRoom()
    local list = {}
    for _, to in ipairs(room.players) do
      list[tostring(to.id)] = { [Player.HistoryGame] = 1 }
    end
    return list
  end,
}

Fk:loadTranslationTable {
  ["lb_zero__xiasheng"] = "侠声",
  [":lb_zero__xiasheng"] = "每名角色限一次，当一名角色脱离濒死后：若导致的伤害来源为你，你可以再对其造成1点伤害；若脱离的回复来源为你，你可以再令其回复1点体力。",

  ["@[skill_players]lb_zero__xiasheng"] = "侠声",

  ["#lb_zero__xiasheng-choose"] = "侠声：你可以对 %src 造成1点伤害或令其回复1点体力",
  ["#lb_zero__xiasheng_damage-ask"] = "侠声：你可以对 %src 造成1点伤害",
  ["#lb_zero__xiasheng_recover-ask"] = "侠声：你可以令 %src 回复1点体力",

  ["#lb_zero__xiasheng_damage"] = "造成1点伤害",
  ["#lb_zero__xiasheng_recover"] = "回复1点体力",

  ["$lb_zero__xiasheng1"] = "Rua！",
  ["$lb_zero__xiasheng2"] = "听听我的虎啸！",
  ["$lb_zero__xiasheng3"] = "最后一招！",
}

skel:addEffect(fk.AfterDying, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and target:isAlive() and skel:withinBranchTimesLimit(player, tostring(target.id), Player.HistoryGame) then
      local room = player.room
      local choices = {}
      if data.damage and data.damage.from == player then
        table.insert(choices, "#lb_zero__xiasheng_damage")
      end
      local recover_event = room.logic:getEventsByRule(GameEvent.Recover, 1, function(e)
        local recover = e.data
        return recover.who == target and not recover.prevented
      end)
      if #recover_event == 1 and recover_event[1].data.recoverBy == player then
        table.insert(choices, "#lb_zero__xiasheng_recover")
      end
      if #choices > 0 then
        event:setCostData(self, { choices = choices })
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = event:getCostData(self).choices
    if not choices or #choices == 0 then return end
    local choice = choices[1]
    if #choices > 1 then
      table.insert(choices, "Cancel")
      choice = room:askToChoice(player, {
        choices = choices,
        skill_name = skel.name,
        prompt = "#lb_zero__xiasheng-choose:" .. target.id,
      })
      if choice == "Cancel" then return end
    else
      if not room:askToSkillInvoke(player, { skill_name = skel.name, prompt = choice .. "-ask:" .. target.id }) then
        return
      end
    end
    event:setCostData(self, { choice = choice })
    return true
  end,
  on_use = function(self, event, target, player, data)
    player:addSkillBranchUseHistory(skel.name, tostring(target.id))
    player:broadcastSkillInvoke(skel.name)
    local room = player.room
    if event:getCostData(self).choice == "#lb_zero__xiasheng_damage" then
      room:notifySkillInvoked(player, skel.name, "offensive", {target})
      room:damage {
        from = player,
        to = target,
        skillName = skel.name,
        damage = 1,
      }
    else
      room:notifySkillInvoked(player, skel.name, "support", {target})
      room:recover {
        who = target,
        skillName = skel.name,
        num = 1,
        recoverBy = player,
      }
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__xiasheng",
    { name = skel.name, scope = Player.HistoryGame, players = table.map(player.room.players, Util.IdMapper) })
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__xiasheng", 0)
end)

return skel
